export default class Acceleration2D {
    constructor(acceleration = 0, angle = 0) {
        this.verticalAcceleration = acceleration * Math.cos((180 - angle) * Math.PI / 180);
        this.horizontalAcceleration = acceleration * Math.sin((180 - angle) * Math.PI / 180);
        Object.defineProperties(this, {
            acceleration: {
                get: () => {
                    return Math.sqrt(Math.pow(this.verticalAcceleration, 2) + Math.pow(this.horizontalAcceleration, 2));
                },
                set: (acceleration) => {
                    if (acceleration < 0) {
                        acceleration = 0;
                    }
                    this.verticalAcceleration = acceleration * Math.cos((180 - this.angle) * Math.PI / 180);
                    this.horizontalAcceleration = acceleration * Math.sin((180 - this.angle) * Math.PI / 180);
                }
            },
            angle: {
                get: () => {
                    return 90 + Math.atan2(this.verticalAcceleration * 1000000, this.horizontalAcceleration * 1000000) * 180 / Math.PI
                },
                set: (angle) => {
                    this.verticalAcceleration = this.acceleration * Math.cos((180 - angle) * Math.PI / 180);
                    this.horizontalAcceleration = this.acceleration * Math.sin((180 - angle) * Math.PI / 180);
                }
            }
        });
    }

    /**
     * @param acceleration {Acceleration2D}
     * @param point {Point2D}
     * @param movement {Movement2D}
     */
    mergeAcceleration(acceleration, point, movement) {
        acceleration.appliedTo(this, point, movement);
    }

    /**
     * @param acceleration {Acceleration2D}
     * @param point {Point2D}
     * @param movement {Movement2D}
     */
    appliedTo(acceleration, point, movement) {
        acceleration.horizontalAcceleration += this.horizontalAcceleration;
        acceleration.verticalAcceleration += this.verticalAcceleration;
    }
}